package cn.edu.hhu.servlet.student;

import cn.edu.hhu.Bean.Student;
import cn.edu.hhu.service.Impl.StudentServiceImpl;
import cn.edu.hhu.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @anthor: lddd
 * @date: 2020/5/8 - 13:17
 * @description:
 **/
@WebServlet("/student/modifyInformationServlet")
public class ModifyInformationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1设置编码
        request.setCharacterEncoding("utf-8");
        //2获取数据
        Student student = (Student)request.getSession().getAttribute("loginStudent");
        String email = request.getParameter("email");
        String tel = request.getParameter("tel");
        String pwd = request.getParameter("pwd");
        //3修改对应的值
        student.setEmail(email);
        student.setTel(tel);
        //3.1若密码字段为空，保持原密码
        if (pwd != null && !pwd.isEmpty()){
            student.setPwd(pwd);
        }
        //4调用Service修改
        System.out.println(student);
        StudentService studentService = new StudentServiceImpl();
        int update = studentService.modifyStudentInformation(student);
        //5跳转到消息页面
        if (update == 1 ){
            //更新成功
            request.getSession().setAttribute("loginServlet",student);
            request.setAttribute("message","修改个人信息成功！");
        }
        else{
            //更新失败
            request.setAttribute("message","修改个人信息失败！");
        }
        System.out.println("修改信息页面"+request.getContextPath());
        request.getRequestDispatcher("/student/message.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
